﻿using System.Linq;
using Entity = Vinno.Brand.Pack.Core.Entities.Suggest;
using IContractService = Vinno.Brand.Pack.Core.IBrandService;
using InputDto = Vinno.Brand.Pack.Core.Dtos.SuggestInputDto;
using OutputDto = Vinno.Brand.Pack.Core.Dtos.SuggestOutputDto;

namespace Vinno.Brand.Apis.Areas.Admin.Controllers
{
    [Description("反馈建议信息")]
    [ModuleInfo(OrderNo = 7, Position = "Brand", PositionName = "小程序模块")]
    public class SuggestController : AdminControllerBase
    {
        /// <summary>
        /// 获取 服务接口
        /// </summary>
        protected IContractService ContractService => ServiceProvider.GetService<IContractService>();

        /// <summary>
        /// 读取
        /// </summary>
        [HttpPost]
        [Description("读取")]
        [ModuleInfo]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = CacheService.ToPageCache<Entity, OutputDto>(ContractService.Suggests, predicate, request.PageCondition,
               function);

            return page.ToPageData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("新增")]
        [ModuleInfo]
        public async Task<AjaxResult> Create(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.CreateSuggests(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”创建成功");
        }

        /// <summary>
        /// 更新
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        [ModuleInfo]
        public async Task<AjaxResult> Update(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.UpdateSuggests(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”更新成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        [ModuleInfo]
        public async Task<AjaxResult> Delete(Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            var result = await ContractService.DeleteSuggests(ids);
            return result.ToAjaxResult();
        }


        /// <summary>
        /// 导出
        /// </summary>
        [HttpPost]
        [Description("导出")]
        [ModuleInfo]
        public AjaxResult Export(FilterGroup group)
        {
            Check.NotNull(group, nameof(group));
            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(group);
            var data = ContractService.Suggests.Where(predicate).MapToList<Entity, OutputDto>();
            return AjaxResult.Success(data);
        }

    }
}